﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Google.GData.Apps;

namespace Google.Apps.MMC.Users
    {
    public partial class UserGroups : CustomControl
        {
        private CustomPropertyPage parent;
        private UserNode user;
        
        private List<string> origgroups = new List<string>();
        private List<string> newgroups = new List<string>();
        private List<string> delgroups = new List<string>();
        private Dictionary<string, string> allgroups;

        public UserGroups(UserNode user, CustomPropertyPage parent)
            {
            this.parent = parent;
            this.user = user;
            InitializeComponent();
            AppsExtendedFeed groups = GoogleAppsSnapIn.appservice.Groups.RetrieveGroups(user.userentry.Login.UserName, true);
            if (groups.Entries.Count != 0)
                {
                foreach (AppsExtendedEntry group in groups.Entries)
                    {
                    this.origgroups.Add(group.getPropetyValueByName("groupName"));
                    this.grouplist.Items.Add(group.getPropetyValueByName("groupName"));
                    }

                }
            }

        private void add_Click(object sender, EventArgs e)
            {
                if (this.allgroups == null)
                    {
                    fetchAllGroups();
                    }
            AddToGroup dialog = new AddToGroup(this.origgroups, this.newgroups, this.delgroups, this.allgroups);
            if (this.parent.ParentSheet.ShowDialog(dialog) == DialogResult.OK)
                {
                    this.parent.Dirty = true;
                    foreach (string group in dialog.groups.SelectedItems)
                        {
                            if (this.delgroups.Contains(group))
                                {
                                this.delgroups.Remove(group);
                                }
                            if (!this.newgroups.Contains(group) && !this.origgroups.Contains(group))
                                {
                                this.newgroups.Add(group);
                                }
                            this.grouplist.Items.Add(group);
                        }
                }
            }

        private void remove_Click(object sender, EventArgs e)
            {
            if (this.grouplist.SelectedItems.Count > 0)
                {
                this.parent.Dirty = true;
                string[] groups = new string[this.grouplist.SelectedItems.Count];
                this.grouplist.SelectedItems.CopyTo(groups, 0);
                foreach (string group in groups)
                    {
                        if (origgroups.Contains(group))
                            {
                            this.delgroups.Add(group);
                            }
                        else if (this.newgroups.Contains(group))
                            {
                            this.newgroups.Remove(group);
                            }
                    this.grouplist.Items.Remove(group);
                    }
                }
            }

        public override bool Apply()
            {
            if (this.parent.Dirty)
                {
                if (this.allgroups == null)
                    {
                    fetchAllGroups();
                    }
                foreach (string newgroup in this.newgroups)
                    {
                    GoogleAppsSnapIn.appservice.Groups.AddMemberToGroup(this.user.userentry.Login.UserName, this.allgroups[newgroup]);
                    }
                foreach (string rmgroup in this.delgroups)
                    {
                        if (GoogleAppsSnapIn.appservice == null) { System.Windows.Forms.MessageBox.Show("GoogleAppSnapIn.appservice"); }
                        if (GoogleAppsSnapIn.appservice.Groups == null) { System.Windows.Forms.MessageBox.Show("GoogleAppSnapIn.appservice.Groups"); }
                        if (this.user == null) { System.Windows.Forms.MessageBox.Show("this.user"); }
                        if (this.user.userentry == null) { System.Windows.Forms.MessageBox.Show("this.user.userentry"); }
                        if (this.user.userentry.Login == null) { System.Windows.Forms.MessageBox.Show("this.user.userentry.Login"); }
                        if (this.user.userentry.Login.UserName == null) { System.Windows.Forms.MessageBox.Show("this.user.userentry.Login.UserName"); }
                        if (this.delgroups == null) { System.Windows.Forms.MessageBox.Show("this.delgroups"); }
                        if (this.allgroups == null) { System.Windows.Forms.MessageBox.Show("this.allgroups"); }
                    GoogleAppsSnapIn.appservice.Groups.RemoveMemberFromGroup(this.user.userentry.Login.UserName, this.allgroups[rmgroup]);
                    }
                this.parent.Dirty = false;
                return true;
                }
            else
                {
                return true;
                }
            }

        public override bool CanApplyChanges()
            {
                return true;
            }

        private void fetchAllGroups()
            {
            Dictionary<string, string> allgroups = new Dictionary<string, string>();
            AppsExtendedFeed groups = GoogleAppsSnapIn.appservice.Groups.RetrieveAllGroups();
            foreach (AppsExtendedEntry group in groups.Entries)
                {
                allgroups.Add(group.getPropetyValueByName("groupName"), group.getPropetyValueByName("groupId"));
                }
            this.allgroups = allgroups;
            }
        }
    }
